/**
 * 
 */
package admin.mode;

import java.util.ArrayList;
import java.util.HashMap;

import base.Power;

/**
 * @author qsj
 * 
 */
public class AdminMode {
	public static final int NAME_ERROR = 1;

	public static final int PASSWORD_ERROR = 2;

	public static final int LOGIN_SUCCESS = 3;

	public static final int LOGIN_ERROR = 4;

	public static final int LOGIN_ADMIN = 5;
	private Power power;

	@SuppressWarnings("unchecked")
	private HashMap userinfo;
	public static String session_name = "user_session_info";

	@SuppressWarnings("unchecked")
	public AdminMode(Power power) {
		this.power = power;
		if (power.getSession().getAttribute(session_name) != null) {
			this.userinfo = (HashMap) power.getSession().getAttribute(
					session_name);
		}
	}

	@SuppressWarnings("unchecked")
	public HashMap getUserInfo() {
		return this.userinfo;
	}

	public boolean checkLogin() {
		return (userinfo != null);
	}

	public void logout() throws Exception {
		if (checkLogin()) {
			power.getSession().invalidate();

		}
	}

	public int login() throws Exception {
		String userName = power.getParameter("username");
		if (userName.equals("")) {
			return NAME_ERROR;
		}
		// MD5Util cipher = new MD5Util();
		// String password =
		// cipher.generatePassword(power.getParameter("password"));
		String password = power.getParameter("password");
		if (password.equals("")) {
			return PASSWORD_ERROR;
		}
		return creatSession(power, userName, password);

	}

	public int creatSession(Power power, String userName, String password)
			throws Exception {
		String sql = "select * from `users` where username='" + userName + "'";
		ArrayList<HashMap<String, String>> list = power.getListFromSql(sql);
		System.out.println("1-----------------------::::" + list.size());
		if (list.size() != 1) {
			return NAME_ERROR;
		} else {
			if (list.get(0).get("PASSWORD").equals(password)) {
				System.out.println("2222-----------------------::::"
						+ list.get(0));
				// 用户基本信息
				userinfo = list.get(0);
				// // 用户组对应的权限（前台）
				// String group_perms_sql =
				// "select * from houseclub_group_permission where group_id="
				// + userinfo.get("GROUP_ID");
				// ArrayList<HashMap<String, String>> groupPermsList = power
				// .getListFromSql(group_perms_sql);
				// userinfo.put("groupPermsList", groupPermsList);
				// userinfo.put("membermenu", new
				// MemberMenuMode().getMemberMenu(
				// power, userinfo.get("GROUP_ID").toString(), userinfo
				// .get("G_SUPER").toString()));
				// if (userinfo.get("IS_ADMIN").equals("1")
				// && !userinfo.get("ROLE_ID").equals("0")) {
				// userinfo
				// .putAll(power
				// .getMapFromSql("select * from houseclub_role where role_id="
				// + userinfo.get("ROLE_ID")));
				// // 用户角色对应的权限（后台）
				// String perms_sql =
				// "select * from houseclub_role_permission where role_id="
				// + userinfo.get("ROLE_ID");
				// ArrayList<HashMap<String, String>> permsList = power
				// .getListFromSql(perms_sql);
				// userinfo.put("permsList", permsList);
				// }
				// power.getSession().setAttribute(session_name, userinfo);
				//
				// power.getSession().setMaxInactiveInterval(60 * 60 * 24);
				// // 更新
				// String sql1 =
				// "update houseclub_user set logindate=now(),logintimes=logintimes+1,loginip='"
				// + base.tools.Tools.getIpAddr(power)
				// + "' where userid="
				// + userinfo.get("USERID");
				// power.executeUpdate(sql1);

				return LOGIN_SUCCESS;
			} else {
				return PASSWORD_ERROR;
			}
		}
	}

	@SuppressWarnings("unchecked")
	public boolean permistionPass(Power power, String opt, HashMap userInfo,
			boolean viewOnly) {
		// 角色是super直接pass
		if (userInfo.get("G_SUPER").equals("1"))
			return true;
		ArrayList<HashMap<String, String>> permsList = (ArrayList<HashMap<String, String>>) userInfo
				.get("permsList");
		for (int i = 0; i < permsList.size(); i++) {
			if (opt.startsWith(permsList.get(i).get("URI").toString())) {
				// 只读管理员
				if (viewOnly) {
					// 操作型地址被禁止
					if (opt.startsWith("o_")) {
						return false;
					}
				}
				return true;
			}
		}
		return false;

	}

	// 前台用
	@SuppressWarnings("unchecked")
	public boolean permistionPass(Power power, String opt, HashMap userInfo) {
		// 角色是super直接pass
//		System.out.println("4-----------------------::::" + userInfo);
//		if (userInfo.get("G_SUPER").equals("1"))
//			return true;
//		ArrayList<HashMap<String, String>> permsList = (ArrayList) userInfo
//				.get("groupPermsList");
//		for (int i = 0; i < permsList.size(); i++) {
//			if (opt.startsWith(permsList.get(i).get("URI").toString())) {
//				return true;
//			}
//		}
		return true;

	}

	@SuppressWarnings("unchecked")
	public int creatGuestSession(Power power) throws Exception {
		String sql = "select * from `users` where username='"
				+ power.getParameter("username") + "'";
		ArrayList<HashMap<String, String>> list = power.getListFromSql(sql);
		System.out.println("``````````````````````````--------::::");
		// 用户基本信息
		if (list.size() != 1) {
			return NAME_ERROR;
		}
		System.out.println("3-----------------------::::" + list.get(0));
		userinfo = list.get(0);
		// 用户组对应的权限（前台）
//		String group_perms_sql = "select * from houseclub_group_permission where group_id="
//				+ userinfo.get("GROUP_ID");
//		ArrayList<HashMap<String, String>> groupPermsList = power
//				.getListFromSql(group_perms_sql);
//		userinfo.put("groupPermsList", groupPermsList);
//		power.getSession().setAttribute(session_name, userinfo);
//		String sql1 = "update houseclub_user set logindate=now(),logintimes=logintimes+1 where userid="
//				+ userinfo.get("USERID");
//		power.executeUpdate(sql1);
		return LOGIN_SUCCESS;
	}

}
